package com.hyyt.shopjsp.basic.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicOplog;
import com.hyyt.shopjsp.basic.service.IBasicOpLogService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/21 0024下午 3:10.
 */
@Controller
@RequestMapping("/back/opLog")
public class BasicOplogController extends BaseController {
	private Logger log = Logger.getLogger(BasicOplogController.class);
	private String basePath = "/back/basic/oplog/";

	@Resource
	private IBasicOpLogService basicOplogService;//操作日志service

	/**
	 * 进入操作日志页面
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("goToOpLog")
	public String goToOpLog(){
		return basePath+"opLogList";
	}

	/**
	 * 查询所有数据字典
	 * @param page
	 * @param response
	 * @param opDesc 操作功能
	 * @param startTime 操作开始时间
	 * @param endTime 操作结束时间
	 */
	@RequestMapping(value="listOpLog",method=RequestMethod.POST)
	@ResponseBody
	public void listOpLog(Page page, String opDesc,String startTime, String endTime,HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(opDesc)) {//是否检索名称
			param.put("opDesc", opDesc.trim());
		}
		if (Utils.objectIsNotEmpty(startTime)) {//是否检索类型名称
			param.put("startTime", startTime.trim());
		}
		if (Utils.objectIsNotEmpty(endTime)) {//是否检索类型名称
			param.put("endTime", endTime.trim());
		}
		param.put("sortColumns", "opId desc");
		int totalRecordCount =basicOplogService.countBasicOplog(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<BasicOplog> list =basicOplogService.queryBasicOplog(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据操作日志id获得操作日志对象
     *
     * @param opId
     * @param response
     */
    @RequestMapping(value = "getOpLogObject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getOpLogObject(Integer opId, HttpServletResponse response) {
        if (Utils.objectIsNotEmpty(opId)) {
            BasicOplog basicOplog = basicOplogService.loadBasicOplog(opId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(basicOplog));
        }else {
			return ResultMsg.create(false);
		}
    }

	/**
	 * 根据操作日志id删除操作日志对象
	 */
	@RequestMapping(value = "deleteOpLog",method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteOpLog(Integer opId){
		if (Utils.objectIsNotEmpty(opId)){
			basicOplogService.deleteBasicOplog(opId);
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}
}